<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
		<!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
		<meta name="renderer" content="webkit">
		<!--国产浏览器高速模式-->
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>电力通信管理系统</title>

		<!-- 公共样式 开始 -->
		<link rel="stylesheet" type="text/css" href="../../css/base.css">
		<link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
		<script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<!-- 滚动条插件 -->
		<link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
		<script src="../../framework/jquery-ui-1.10.4.min.js"></script>
		<script src="../../framework/jquery.mousewheel.min.js"></script>
		<script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
		<script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
		<!-- 公共样式 结束 -->
		<script type="text/javascript" src="../../framework/frame.js" ></script>
		<style>
			.layui-form{
				margin-right: 30%;
			}
		</style>

	</head>

	<body>
		<div class="cBody">
				<form id="form" action="" method="post" class="layui-form">
					<div class="layui-form-item">
						<label class="layui-form-label">班组名称</label>
						<div class="layui-input-inline shortInput">
							<input  type="text" id="workname" name="workname" required lay-verify="workname" placeholder="请输入班组名称" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">成员数量</label>
						<div class="layui-input-inline shortInput">
							<input   type="text" id="worknumber" name="worknumber" required lay-verify="worknumber" placeholder="请输入成员数量" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">所属部门</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" lay-filter="workdepartname" style="margin-left: 0px;" name="workdepartname" id="workdepartname" lay-verify="required">
							</select>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">组长</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" style="margin-left: 0px;" name="workleader" id="workleader" lay-filter="workleader" lay-verify="required">
							</select>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" id="btn" lay-submit lay-filter="submitBut">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>
		</div>
	</body>

<script>
	$(document).ready(function(){
		getDept("")
		getUsers("")
	});
	layui.use('form', function() {
		var form = layui.form;
		form.verify({
			workname: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入班组名称！';
				}
			},
			worknumber: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入成员数量！';
				}
			}
		});
		//监听提交
		form.on('submit(submitBut)', function(data) {
			var body=data.field;
			$.ajax({
				type: "post",
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				dataType: "json",
				url: "http://localhost:8888/Workinfo/save",
				contentType: "application/json;charset=UTF-8",//指定消息请求类型
				data: JSON.stringify(body),//将js对象转成json对象
				success: function (data,status,xhr) {
					if (data.code === 0) {
						layer.msg("添加成功");
						$("#form")[0].reset()
						form.render()
					} else {
						layer.msg(data.msg)
					}
					return false
				},
				error:function () {
					layer.msg("添加失败，请稍后再试")
					return false
				}
			})
            return  false
		});
	});
	function  getDept(id) {
		$.ajax({
			url: 'http://localhost:8888/dept/list',
			dataType: 'json',
			type: 'get',
			success: function (data) {
				console.log(data)
				$.each(data.detail, function (index, item) {
					if(id===""){
						$('#workdepartname').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}else{
						$('#workdepartnameSearch').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}
				});
				if(id===""){
					layui.form.render("select");
				}
			}
		})
	}
	function getUsers(id) {
		var user=JSON.parse(window.sessionStorage.getItem("user"));
		var body={
			page:1,
			limit:0,
			id:user.id
		}
		$.ajax({
			url: 'http://localhost:8888/user/list',
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			type: 'post',
			data: body,//将js对象转成json对象
			success: function (data) {
				console.log(data)
				$.each(data.data, function (index, item) {
					if(id===""){
						$('#workleader').append(new Option(item.managername, item.id,null,item.id === id));// 下拉菜单里添加元素
					}else{
						$('#workleaderSearch').append(new Option(item.managername, item.id,null,item.id === id));// 下拉菜单里添加元素
					}
				});
				if(id===""){
					layui.form.render("select");
				}
			}
		})
	}
</script>

</html>
